﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Hosting;
using System.Security.Permissions;
using System.Text;
using webnew.BLL;
using webnew.Model;
using System.Collections.Generic;
namespace webnew
{
    /// <summary>
    ///ExtMenuTree 的例子：
      //var Tree1 = Ext.tree;
      //var tree1 = new Tree1.TreePanel({
      //    id: 'system_2',
      //    title: 'IC卡管理',
      //    animate: true, //开启动画效果   
      //    enableDD: false, //不允许子节点拖动
      //    border: false, //没有边框
      //    autoScroll: true,
      //    autoWidth: true,
      //    style: 'background-color:#FFFFFF;border-color:#0099FF;',
      //    rootVisible: false, //设为false将隐藏根节点，很多情况下，我们选择隐藏根节点增加美观性
      //    loader: new Tree1.TreeLoader({
      //        dataUrl: 'ext_tree_json.aspx '// OrgTreeJsonData.action就是要动态载入数据的请求地址，这里请求时会提交一个参数为node的值，值为root即new Tree.AsyncTreeNode()对象的id值
      //    })
      //});
      //var root1 = new Tree1.AsyncTreeNode({

      //    text: '组织机构树',
      //    draggable: false,
      //    id: '1'//默认的node值：?node=-100
      //});

      //tree1.setRootNode(root1);
      ////tree.render();
      //root1.expand();
      //tree1.on("click", function(node, event) {
      //    TreeClick(node, event);
      //});
    /// </summary>
    public  class ExtMenuTree
    {

       

        /// <summary>
        /// 生成一个EXT一级菜单栏对象
        /// </summary>
        /// <param name="navid">导航id</param>
        /// <param name="picurl">菜单图标</param>
        /// <param name="navname">菜单名</param>
        /// <param name="classcount">菜单栏对象标识，如tree1,tree2</param>
        /// <returns></returns>
        public static string ToTreeMenu(string navid, string picurl, string navname, string classcount)
        {
            StringBuilder strtree = new StringBuilder();
            strtree.Append(@"var Tree" + classcount + " = Ext.tree; var tree" + classcount + " = new Tree" + classcount + ".TreePanel({ id: '" + navid + "',");
            strtree.Append(@"title: " + "\"<img src='App_Themes/Images/Icon/" + picurl + ".gif' align='middle' alt=''/>" + navname + "\",");
            strtree.Append(@"animate: true, enableDD: false, border: false, autoScroll: true, autoWidth: true, style: 'background-color:#FFFFFF;border-color:#0099FF;',rootVisible: false,");
            strtree.Append(@"loader : new Tree" + classcount + ".TreeLoader( { dataUrl: 'ExtTreeJson.aspx?parentid=" + navid + " '})});");
            strtree.Append(@"var root" + classcount + " = new Tree" + classcount + ".AsyncTreeNode({ text : '" + navname + "', draggable : false, id : '" + navid + "' });");
            strtree.Append(@"tree" + classcount + ".setRootNode(root" + classcount + "); root" + classcount + ".expand();");
            strtree.Append(@"tree" + classcount + ".on('click', function(node, event) {TreeClick(node, event);});");
            return strtree.ToString();
        }

        /// <summary>
        /// 生成EXT菜单树
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="parentid"></param>
        /// <returns></returns>
        public static string CreateExtTreeJSON(string userid, string parentid, string strMacName)
        {
            StringBuilder sb = new StringBuilder();
            CreateExtTreeNode(userid, parentid,strMacName, sb);
            string s = sb.ToString();
            return s.Replace("}{", "},{");
        }
        /// <summary>
        /// 根据用户id,父节点ID，获取菜单列表
        /// </summary>
        /// <param name="parentid"></param>
        /// <returns></returns>
        private static List<Model.Sys_NavigationInfo> GetNodesList(string userid, string parentid, string strMacName)
        {
            List<Model.Sys_NavigationInfo> lst = new BLL.Sys_Navigation().GetUserNav(userid, strMacName);
            List<Model.Sys_NavigationInfo> newlst = new List<Model.Sys_NavigationInfo>();
            StringBuilder strTreeMenu = new StringBuilder();
            foreach (Model.Sys_NavigationInfo item in lst)
            {
                if (item.ParentID == parentid)  //可修改顶级父节点
                {
                    newlst.Add(item);
                }
            }
            return newlst;
        }


        /*
         * Ext Tree JSON 数据部分属性说明：
         * text: 要显示的节点文件
         * id:这个就不用解释了
         * href:链接地址
         * expanded:是否展开
         * hrefTarget：链接目标框架名称
         * children:子节点 格式：[{节点1},{节点2}...]
         * leaf:当前节点是否为叶子节点。如果为false 则此节点有子节点。
         *      否则为true,此节点在无子节点
         * */
        /// <summary>
        /// 生成树的所有节点，递归调用AddChildrenNode方法
        /// </summary>
        /// <param name="userid">用户ID</param>
        /// <param name="parentid">父节点ID</param>
        /// <param name="sb">返回的JSON 字符串</param>
        private static void CreateExtTreeNode(string userid, string parentid,string strMacName, StringBuilder sb)
        {
            List<Model.Sys_NavigationInfo> lst = GetNodesList(userid, parentid, strMacName);
            if (lst.Count > 0)
            {
                sb.Append("[");
                foreach (Model.Sys_NavigationInfo item in lst)
                {
                    sb.Append("{");
                    sb.Append("text:'" + item.NaviName + "',");
                    sb.Append("id:'node" + item.Id + "',");
                    sb.Append("href:'" + item.pagesrc + "',");
                    sb.Append("expanded:false,");
                    sb.Append("icon:'/App_Themes/Images/Icon/" + item.picsrc + ".gif'");
                    AddChildrenNode(userid, GetNodesList(userid, item.Id, strMacName), sb, strMacName);
                    sb.Append("}");
                }
            }

            sb.Append("]");
        }

        /// <summary>
        /// 添加父节点下所有的子节点
        /// </summary>
        /// <param name="lst"></param>
        /// <param name="sb"></param>
        private static void AddChildrenNode(string userid,List<Model.Sys_NavigationInfo> lst, StringBuilder sb,string strMacName)
        {
            if (lst.Count > 0)
            {
                sb.Append(",leaf:false,children:[");
                foreach (Model.Sys_NavigationInfo item in lst)
                {
                    sb.Append("{");
                    sb.Append("text:'" + item.NaviName + "',");
                    sb.Append("expanded:false,");
                    sb.Append("id:'node" + item.Id + "',");
                    sb.Append("href:'" + item.pagesrc + "',");
                    sb.Append("icon:'/App_Themes/Images/Icon/" + item.picsrc + ".gif'");
                    AddChildrenNode(userid, GetNodesList(userid, item.Id,strMacName), sb,strMacName);
                    sb.Append("}");
                }

                sb.Append("]");
            }
            else
                
                sb.Append(",leaf:true");
        }
    }
}
